package com.jerehedu.myapplication34.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.jerehedu.myapplication34.entrty.Book;
import com.jerehedu.myapplication34.openhelper.DataBaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据库对book的一些操作
 * Created by lenovo on 2016/11/4.
 */
public class BookDao {
    private Dao<Book,String> bookDao;
    public BookDao(Context context){
        DataBaseHelper dataBaseHelper=new DataBaseHelper(context);
        try {
            bookDao=dataBaseHelper.getDao(Book.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //查询所有数据库中的书籍
    public List<Book> findAllBook(){
        try {
            return bookDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //查询书籍是否在本地库存在
    public boolean checkBookIsExist(Book book){
        List<Book> list=findAllBook();
        boolean flag=false;
        for(Book b:list){
            if(b.getName().equals(book.getName())
                    &&b.getUrl().equals(book.getUrl())
                    ){
                flag=true;
                break;
            }
        }
        return flag;
    }

    //添加一本书籍
    public long addBook(Book book){
        boolean flag=checkBookIsExist(book);
        int idAdd=0;
        if(!flag){
            try {
                idAdd=bookDao.create(book);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return idAdd;
    }

    //添加多本书籍
    public long addBooks(List<Book> list){
        ArrayList<Book> list1=new ArrayList<>();
        for(Book book:list){
            boolean flag=checkBookIsExist(book);
            if(!flag){
                list1.add(book);
            }
        }
        int idAdds=0;
        try {
            idAdds=bookDao.create(list1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return idAdds;
    }

    //删除一本书籍
    public void removeBook(Book book){
        boolean flag=checkBookIsExist(book);
        if(flag){
            DeleteBuilder<Book,String> builder=bookDao.deleteBuilder();
            try {
                builder.where().eq("url",book.getUrl());
                builder.delete();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //删除多本书籍
    public void removeBooks(List<Book> list){
        for(Book book:list){
            removeBook(book);
        }
    }

    //修改书籍的阅读进度
    public void updateProgress(int id,int chapter,double progress){
        try {
            Book book=bookDao.queryForId("id");
            if(book!=null){
                book.setChapterNum(chapter);
                book.setProgress(progress);
                bookDao.update(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }




}
